
<div class="layui-fluid">
	<form class="layui-form" action="" lay-filter="goods-list-form" id="goods-list-form">
		   <div class="layui-form-item layui-inline">
                <div class="layui-input-inline">
                  <input type="text" name="no" id="no" class="layui-input" placeholder="请输入订单号">
                </div>
             </div>
            <div class="layui-form-item layui-inline">
                <div class="layui-input-inline">
                  <input type="text" name="start_time" class="layui-input" id="test1" placeholder="提交退回订单开始时间">
                </div>
             </div>
             <div class="layui-form-item layui-inline">
                <div class="layui-input-inline">
                  <input type="text" name="end_time" class="layui-input" id="test2" placeholder="提交退回订单结束时间">
                </div>
             </div>
              <div class="layui-form-item layui-input-inline">
                <input type="text" name="user_id" id="keyword" placeholder="用户昵称或订单手机号" autocomplete="off" class="layui-input">
              </div>
              <div class="layui-form-item layui-inline" style="margin-left:20px;">
                <div class="layui-input-inline">
                  <button class="layui-btn" lay-submit lay-filter="goods-list-search">搜索</button>
                  <!-- <button class="layui-btn layui-btn-primary" lay-filter="goods-list-search1">重置</button>-->
                </div>
              </div>
        </form>

    <div class="layui-row layui-col-space15">
        <div class="layui-col-md12">
        <div class="layui-card">
            <div class="layui-card-body">
            <!--<div class="layui-btn-group" style="margin-bottom: 10px;width: 100%;">
                <button class="layui-btn layuiadmin-btn-tags" style="float: right;" data-type="add">添加书单</button>
            </div>-->

            <table class="layui-hide" id="test-table-operate" lay-filter="test-table-operate"></table>
          <script type="text/html" id="user_avatar">
            <img src="{{d.user_avatar}}" alt="" style="width:80px; height: 80px;border-radius: 50%;">
          </script>
	        <script type="text/html" id="LAY-good-list-table-stock">
	            <p>￥{{d.fee_price}}</p>
	        </script>
	        <script type="text/html" id="LAY-good-list-table-stock1">
	            <p>{{parseInt(d.state) ==2? '发货完成，已退回': '已提交订单，待退回'}}</p>
	        </script>
	        <script type="text/html" id="LAY-good-list-table-stock2">
	            <p>{{d.amount}}本</p>
	        </script>
            <script type="text/html" id="user_name">
				<a lay-event="detail" style="cursor: pointer;color:blue">{{d.user_nickname}}</a>
			</script>
            <script type="text/html" id="test-table-operate-barDemo">
                <a class="layui-btn layui-btn-xs" lay-event="edit">查看</a>
            </script>
            </div>
        </div>
        </div>
    </div>
    </div>
<script>
layui.use(['admin', 'table', 'form', 'upload','laydate'], function(){
	var user_id = layui.router().search.user_id
    var table = layui.table
    ,laydate = layui.laydate
    ,form = layui.form
    ,admin = layui.admin
    ,upload = layui.upload
    ,view = layui.view
    ,element = layui.element
    ,$ = layui.$
	,cols = [
		[
		    {
				align: 'center',
				title: '订单号',
				field: 'no'
			},
			 {
				align: 'center',
				field: 'amount',
				title: '退回本数',
				templet: '#LAY-good-list-table-stock2'
			},
			{
				align: 'center',
				width: 130,
				templet: '#user_avatar',
				title: '卖书用户头像',
				field: 'user_avatar'
			}, {
				align: 'center',
				field: 'user_nickname',
				title: '卖书用户',
				templet: '#user_name'
			}, {
				align: 'center',
				field: 'created_at',
				title: '下单时间'
			},{
				align: 'center',
				field: 'fee_price',
				title: '支付运费',
				templet: '#LAY-good-list-table-stock'
			}, {
				align: 'center',
				field: 'state',
				title: '订单状态',
				templet: '#LAY-good-list-table-stock1'
			},{
				align: 'center',
				fixed: 'right',
				title: '操作',
				toolbar: '#test-table-operate-barDemo'
			}
		]
	];
	  laydate.render({
    elem: '#test1'
     ,type: 'datetime'
  });
    laydate.render({
    elem: '#test2'
     ,type: 'datetime'
  });
	table.set({
		headers: { //通过 request 头传递
			'access-token': layui.data('layuiAdmin').access_token
		}
	});
	table.render({
		elem: '#test-table-operate',
		url: 'https://bookshop.dingdangtoy.com/api/admin/sell/order/backs?state=1&user_id='+ user_id,
		parseData: function(res) { //res 即为原始返回的数据
			return {
				"code": res.code, //解析接口状态
				"msg": res.message, //解析提示文本
				"data": res.data.items, //解析数据列表
				"count": res.data._meta.total_count //解析数据长度
			}
		},
		cols: cols,
		page: true
	});

    //监听工具条
    table.on('tool(test-table-operate)', function(obj){
      var data = obj.data;
      if(obj.event === 'edit'){
        admin.popup({
        title: '查看退回订单'
        ,area: ['80%', '96%']
        ,id: 'LAY-popup-content-tags'
        ,success: function(layero, index){
          view(this.id).render('return-order/wait/add', data).done(function(){
          	    if(parseInt(data.state) ==1) {
          	    	$('#ppx1').addClass('active')
          	    	$('#div3').show();
          	    } else {
          	    	$('#div4').show();
          	    }
          	    // 点击预约发货
				$('#detection').on('click', function(){
					layer.confirm('请确认是否已打印运单，并将货物打好包裹等待快递上门取件？', function(index){
			          //执行 Ajax 后重载
					    $.ajax({
						    type: "POST",
						    dataType: "json",
						    url: "https://bookshop.dingdangtoy.com/api/admin/sell/order/back/send",
						    data: {
						    	order_id: data.id
						    },
						    headers: { //通过 request 头传递
					          'access-token': layui.data('layuiAdmin').access_token
					        },
							success: function(data){
					            if (parseInt(data.code) == 0) {
							    	layer.msg('操作成功');
								    layui.table.reload('test-table-operate');
								    setTimeout(function(){
				                        window.parent.layer.closeAll();
				                    },1000)
							    } else {
							    	layer.msg(data.message);
							    }
							},
							error: function(msg){
							  console.log(msg)
							}
						});
				        layer.close(index);
				   });
					return false;
			  });
	   	        $.ajax({
				    type: "GET",
				    dataType: "json",
				    url: "https://bookshop.dingdangtoy.com/api/admin/sell/order/back?order_id=" + data.id,
				    data: {},
				    headers: { //通过 request 头传递
		              'access-token': layui.data('layuiAdmin').access_token
		            },
							success: function(data){
							    if (parseInt(data.code) == 0) {
							    	$('#no1').text(data.data.no)
							    	$('#address_receiver').text(data.data.address_receiver)
							    	$('#address_mobile').text(data.data.address_mobile)
							    	$('#address_province_name').text(data.data.address_province_name)
							    	$('#address_city_name').text(data.data.address_city_name)
							    	$('#address_area_name').text(data.data.address_area_name)
							    	$('#address').text(data.data.address)
							    	$('#created_at').text(data.data.created_at)
							    	$('#send_time').text(data.data.send_time)
							    	$('#amount').text(data.data.amount)
							    	$('#fee_price').text(data.data.fee_price)
							    	$('#express_delivery_id').text(data.data.express_delivery_id)
							    	if (data.data.express_delivery_id == 'ZTO') {
							    	    $('#express_delivery_id').text('中通快递')
							    	} else {
							    		$('#express_delivery_id').text('')
							    	}
							    	$('#express_no').text(data.data.express_no)
							    	$('#user_nickname').text(data.data.user_nickname)
							    	$('#user_avatar1').attr('src',data.data.user_avatar)
                                    var _html1 = '';
								    for(var i = 0; i < data.data.books.length; i++) {
										_html1 += '<tr><td style="width:100px"><img style="width:80px;height:100px" src="'+data.data.books[i].book_image+'" alt="" /></td><td>'+data.data.books[i].book_title+'</td><td>'+data.data.books[i].book_author+'</td><td>'+data.data.books[i].isbn+'</td><td>'+data.data.books[i].reason+'</td></tr>';
								    }
								    $('#table1').append(_html1)

							    	form.render();
							    } else {
							    	layer.msg(data.message);
							    }
							},
							error: function(msg){
							  console.log(msg)
							}
					});

          });
        }
      });
      } else if (obj.event === 'detail') {
				admin.popup({
					title: '查看用户',
					area: ['80%', '96%'],
					id: 'LAY-popup-content-tags',
					success: function(layero, index) {
						view(this.id).render('user-manage/all/add', data).done(function() {
							var is_black = '';
							var titles = '';
							var contents = '';
							// 跳转收书列表
							$('#order_books1').click(function() {
								location.hash = '/buy-orders/all/index/user_id=' + data.user_id
								return false;
							})
							// 跳转卖书列表
							$('#order_books').click(function() {
								location.hash = '/sale-orders/all/index/user_id=' + data.user_id
								return false;
							})
							// 跳转订单退回列表
							$('#order_books2').click(function() {
								location.hash = '/return-order/order/index/user_id=' + data.user_id
								return false;
							})
							$.ajax({
								type: "GET",
								dataType: "json",
								url: "https://bookshop.dingdangtoy.com/api/admin/user/show?id=" + data.user_id,
								data: {},
								headers: { //通过 request 头传递
									'access-token': layui.data('layuiAdmin').access_token
								},
								success: function(data) {
									if(parseInt(data.code) == 0) {
										$('#wechat_avatar1').attr('src', data.data.wechat_avatar)
										$('#wechat_nickname').text(data.data.wechat_nickname)
										$('#account').text(data.data.account)
										$('#created_at').text(data.data.created_at)
										$('#recommend_users_success').text(data.data.recommend_users_success)
										$('#buy_orders_num').text(data.data.buy_orders_num)
										$('#buy_books_num').text(data.data.buy_books_num)
										$('#sell_orders_num').text(data.data.sell_orders_num)
										$('#sell_books_num1').text(data.data.sell_books_num1)
										$('#sell_books_num2').text(data.data.sell_books_num2)
										$('#back_orders_num').text(data.data.back_orders_num)
										$('#back_books_num').text(data.data.back_books_num)
										$('#get_money').text(data.data.get_money)

										if(parseInt(data.data.is_official_account) == 1) {
											$('#wechat_nickname1').text('已关注公众号')
										} else {
											$('#wechat_nickname1').text('未关注公众号')
										}
										if(parseInt(data.data.is_black) == 1) {
											is_black = 0;
											$('#blacklist').text('取消标记黑名单');
											$('#blacklistUser').show();
											titles = '确认将用户取消标记';
											contents = '确认后，该用户的卖书权限将恢复，是否确认操作';
										} else {
											is_black = 1;
											$('#blacklist').text('标记为黑名单用户');
											titles = '确认将用户标记为黑名单';
											contents = '提交后，该用户卖书下单时将会被系统自动拒绝是否确认操作？';
										}
										$('#recommend_users_total').text(data.data.recommend_users_total)
										if(data.data.addresses.length > 0) {
											$('#base-address').show();
										}
										var _html = '';
										var _html1 = '';
										var _html2 = '';
										var num = 0;
										for(var i = 0; i < data.data.addresses.length; i++) {
											num += 1
											_html += '<li><p>地址信息' + num + '</p><p><span style="margin-right:20px">' + data.data.addresses[i].receiver + '</span>' + data.data.addresses[i].mobile + '</p><p>' + data.data.addresses[i].province_name + '' + data.data.addresses[i].city_name + '' + data.data.addresses[i].area_name + '' + data.data.addresses[i].address + '</p></li>';
										}
										$('#address-list').append(_html)
										for(var i = 0; i < data.data.account_records1.length; i++) {
											_html1 += '<tr><td>卖书（' + data.data.account_records1[i].books_num + '本）</td><td>' + data.data.account_records1[i].created_at + '</td><td>' + data.data.account_records1[i].platform_success_time + '</td><td>' + data.data.account_records1[i].actual_price + '</td></tr>';
										}
										$('#table1').append(_html1)
										for(var i = 0; i < data.data.account_records2.length; i++) {
											if(parseInt(data.data.account_records2[i].state) == 1) {
												_html2 += '<tr><td>' + data.data.account_records2[i].remark + '</td><td>' + data.data.account_records2[i].created_at + '</td><td>' + data.data.account_records2[i].account + '</td><td>提现成功</td></tr>';
											} else {
												_html2 += '<tr><td>' + data.data.account_records2[i].remark + '</td><td>' + data.data.account_records2[i].created_at + '</td><td>' + data.data.account_records2[i].account + '</td><td stle="color:red">提现失败</td></tr>';

											}
										}
										$('#table2').append(_html2)

										form.render();
									} else {
										layer.msg(data.message);
									}
								},
								error: function(msg) {
									console.log(msg)
								}
							});
              // 黑名单加入与取消
							$('#blacklist').click(function() {
									//执行 Ajax 后重载
									layer.open({
							        type: 1
							        ,title: titles
							        ,area: '300px;'
							        ,shade: 0.8
							        ,id: 'LAY_layuipro'
							        ,btn: ['确认','取消',]
							        ,btnAlign: 'c'
							        ,moveType: 1 //拖拽模式，0或者1
							        ,content: '<div style="padding: 50px; line-height: 22px; background-color: #fff; color: #000; font-weight: 300;"><p>'+contents+'</p></div>'
							        ,success: function(layero){
							          var btn = layero.find('.layui-layer-btn');
							          btn.find('.layui-layer-btn0').click(function(){
													$.ajax({
															type: "POST",
															dataType: "json",
															url: "https://bookshop.dingdangtoy.com/api/admin/user/black",
															data: {
																'user_id':data.user_id,
																'is_black':is_black,
															},
															headers: {
																		'access-token': layui.data('layuiAdmin').access_token
																	},
														success: function(data){
															if(data.code == 0){
																layer.msg('操作成功');
																layui.table.reload('test-table-operate');
																setTimeout(function(){
																		window.parent.layer.closeAll();
																},1000)
															} else {
																layer.msg(data.message);
															}

														},
														error: function(msg){
															console.log(msg)
														}
													});
							          })
							        }
							      });
								return false;


							})
						});
					}
				});
			}
    });
	// 搜索 ,
	form.on('submit(goods-list-search)', function(data) {
			table.reload('test-table-operate', {
				page: {
					curr: 1 //重新从第 1 页开始
				},
				where: {
					'no':$('#no').val(),
					'start_time': $('#test1').val(),
					'end_time': $('#test2').val(),
					'user_id': $('#keyword').val()
				}
			});
		return false;
	});

  });
</script>
<style type="text/css">
	.layui-table-cell, .layui-table-tool-panel li{white-space: inherit !important;}
</style>
